package com.wjb.xietong.server.crm;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.wjb.xietong.datamanager.LoginUserDataManager;
import com.wjb.xietong.server.Msg;
import com.wjb.xietong.server.crm.SalesInfo.SalesData;
import com.wjb.xietong.server.crm.SellerStatis.SellerStatisInfo;
import com.wjb.xietong.util.IDs;
import com.wjb.xietong.util.LogD;
import com.wjb.xietong.util.EnumDefine.Login_User_Info;

public class SellerInfo extends RequestBase{
	
	private LinkedList<Seller> listSeller = null;
	
	
	public SellerInfo(){
		msgType = Msg.TYPE_CRM_SELLER;
	}
	
	
	public class Seller{
		public long id;
		public String nick;
		public long type;
		
		public SellerStatisInfo sellerStatis;
		
		public LinkedList<SalesData> listSales;
		
		public String getTypeString(){
			switch((int)type){
			case 1:
				return "淘宝";
			case 2:
				return "拍拍";
			case 3:
				return "京东";
			case 4:
				return "亚马逊";
			case 8:
				return "天猫";
			case 5:
				return "一号店";
			case 6:
				return "当当";
			case 7:
				return "自建";
			case 11:
				return "微淘";
			case 12:
				return "shopEx";
			case 13:
				return "ecShop";
			case 14:
				return "聚美优品";
			case 15:
				return "数据中心";
			case 16:
				return "千牛";
			default:
				return "未知";
			}
		}
		
		public Seller(){
			sellerStatis = new SellerStatisInfo();
			listSales = new LinkedList<SalesData>();
		}
	}
	

	public Msg requestSellerInfo(LinkedList<Seller> list){
		
		listSeller = list;
		
		requsetInfo(IDs.CRM_URL);
		
		return getMsg();
	}
	

	
	@Override
	public Map<String, String> getRqstMap() {
		Map<String, String> map = new HashMap<String, String>();
		map.put(IDs.M, IDs.NOTIFY_CRM_SELLER);
		map.put(IDs.COM_ID, LoginUserDataManager.getLoginUserInfo(Login_User_Info.COM_ID).toString());
		map.put(IDs.USER_ID, LoginUserDataManager.getLoginUserInfo(Login_User_Info.ID).toString());
		return map;
	}
	
	@Override
	public int jsonGetResp(JSONObject sellerInfoResp){
		if (!checkRespCode(sellerInfoResp)) {
			// TODO
			return -1;
		}
		try {
			if (sellerInfoResp.has(IDs.CRM_SELLER_MAP_LIST)) {
				JSONArray array = sellerInfoResp
						.getJSONArray(IDs.CRM_SELLER_MAP_LIST);
				if (null == array) {
					LogD.output("Warning: No Data!");
					return -3;
				}

				for (int i = 0; i < array.length(); i++) {
					JSONObject jsonObj = array.getJSONObject(i);
					if (null == jsonObj) {
						continue;
					}

					Seller info = new Seller();
					if(jsonObj.has(IDs.CRM_SELLER_ID)){
						info.id = jsonObj.getLong(IDs.CRM_SELLER_ID);
					}
					if(jsonObj.has(IDs.CRM_SELLER_NICK)){
						info.nick = new String(jsonObj.getString(IDs.CRM_SELLER_NICK));
					}
					if(jsonObj.has(IDs.CRM_SELLER_TYPE)){
						info.type = jsonObj.getLong(IDs.CRM_SELLER_TYPE);
					}
					listSeller.add(info);
				}
			}
		} catch (JSONException e) {
			LogD.output("Exception:" + e.getMessage());
			return -2;
		}
		return 0;
	}
	
	@Override
	public void initData() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean parseJsonObj(JSONObject response) {
		// TODO Auto-generated method stub
		return false;
	}




}
